{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import scanpy as sc\n",
    "import anndata\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "\n",
    "data_type = 'float32'\n",
    "\n",
    "import cell2location\n",
    "\n",
    "import matplotlib as mpl\n",
    "from matplotlib import rcParams\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# silence scanpy that prints a lot of warnings\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sc_data = sc.read_loom(\"/net/data.isilon/ag-saez/bq_rramirez/visiumMI_revisions/results/integration/integrated_data_wstates.loom\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'sc' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-c104641ae98c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# remove cells and genes with 0 counts everywhere\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilter_cells\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msc_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmin_genes\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0msc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilter_genes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msc_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmin_cells\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'sc' is not defined"
     ]
    }
   ],
   "source": [
    "# remove cells and genes with 0 counts everywhere\n",
    "sc.pp.filter_cells(sc_data, min_genes=1)\n",
    "sc.pp.filter_genes(sc_data, min_cells=1)\n",
    "\n",
    "# calculate the mean of each gene across non-zero cells\n",
    "sc_data.var['n_cells'] = (sc_data.X.toarray() > 0).sum(0)\n",
    "sc_data.var['nonz_mean'] = sc_data.X.toarray().sum(0) / sc_data.var['n_cells']\n",
    "\n",
    "nonz_mean_cutoff = 0.05\n",
    "cell_count_cutoff = np.log10(sc_data.shape[0] * 0.0005)\n",
    "cell_count_cutoff2 = np.log10(sc_data.shape[0] * 0.03)\n",
    "\n",
    "sc_data[:,(np.array(np.log10(sc_data.var['nonz_mean']) > nonz_mean_cutoff)\n",
    "         | np.array(np.log10(sc_data.var['n_cells']) > cell_count_cutoff2))\n",
    "      & np.array(np.log10(sc_data.var['n_cells']) > cell_count_cutoff)].shape\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
